﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PdfSharp.Pdf;
using System.Text.RegularExpressions;
using System.IO;
namespace Cdc
{
    /// <summary>
    /// Interaction logic for Mail.xaml
    /// </summary>
    public partial class Mail : UserControl
    {
        private String fileName;
        public Mail(String fileName)
        {
            this.fileName = fileName;
            InitializeComponent();
        }

        private void send(object sender, RoutedEventArgs e)
        {
            try
            {
                // creation du msg
                System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
                mailMsg.From = new System.Net.Mail.MailAddress("cedric.dagorne@gmail.com", "Cédric DAGORNE");

                String mailRegexp = @"\w+(-\.\w)*@(\w+\.)+\w\w\w?";

                if (ToTb.Text.Length > 0)
                {
                    String[] dests = ToTb.Text.Split(';');
                    foreach (String dest in dests)
                    {
                        if (Regex.IsMatch(dest, mailRegexp))
                        {
                            mailMsg.To.Add(dest);
                        }
                        else
                        {
                            MessageBox.Show("Error to wrong mail format in the recipients field");
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("You must specify at least one recipient");
                    return;
                }

                if (CcTb.Text.Length > 0)
                {
                    String[] dests = CcTb.Text.Split(';');
                    foreach (String dest in dests)
                    {
                        if (Regex.IsMatch(dest, mailRegexp))
                        {
                            mailMsg.To.Add(dest);
                        }
                        else
                        {
                            MessageBox.Show("Error to wrong mail format in the copy recipients field");
                            return;
                        }
                    }
                }

                mailMsg.Subject = this.ObjectTb.Text;
                //MemoryStream ms = new MemoryStream();
                //doc.Save(ms,false);
                //doc.Save(@"c:\test.pdf");
                //ms.Seek(0, SeekOrigin.Begin);
                mailMsg.Attachments.Add(new System.Net.Mail.Attachment(fileName));


                // Envoie du mail
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("srvsmtp.smt.cib.net", 25);
                client.Send(mailMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Technical error occured when sending the mail : \n"+ex.Message);                            
            }
        }
    }
}
